package DAO;

import utility.HibernateUtil;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.Metric_User;
import model.User;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import validator.e;

public class MetricDAO extends BaseDAO<Metric_User> {

    /**
     *
     * @param metric
     */
    @Override
    public void save(Metric_User metric) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();

        if (consult(metric.getName())) {
            try {
                session.save(metric);
                t.commit();
                e.showMsgSuccess("Sucess");
            } catch (HibernateException err) {
                err.printStackTrace();
                e.showMsgError("Error - Try Again");
            }
        } else {
            Query query = session.createQuery("delete from Metric_user");
            query.executeUpdate();
            session.save(metric);
            t.commit();
            e.showMsgSuccess("Sucess");
        }
        session.close();
    }

    public List<Metric_User> glist(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Query query = session.createSQLQuery("select * from Metric_user where category_id = :category_id").addEntity(Metric_User.class);
        query.setParameter("category_id", id);
        List lista = query.list();
        session.close();
        return lista;
    }

    @Override
    public void update(Metric_User metric) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        try {
            session.update(metric);
            t.commit();
            e.showMsgSuccess("Sucess");
        } catch (HibernateException err) {
            e.showMsgError("Error");
        }
        session.close();
    }

    @Override
    public void remove(Metric_User metric) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.delete(metric);
        t.commit();
        session.close();
    }

    @Override
    public boolean consult(String name) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Query query = session.createSQLQuery("select * from Metric_user where name = :name").addEntity(Metric_User.class);
        query.setParameter("name", name);
        List lista = query.list();
        if (lista == null || lista.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<Metric_User> list() {
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession sessions = (HttpSession) fc.getExternalContext().getSession(false);
        User u = (User) sessions.getAttribute("user");
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        System.out.println("ID_USER" + u.getId());
        Query query = session.createSQLQuery("select * from Metric_user where user= :user_id").addEntity(Metric_User.class);
        query.setParameter("user_id", u.getId());
        List lista = query.list();
        session.close();
        return lista;
    }
}
